using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NiSis.Data;
using NiSis.Entities;
using NiSis.Loggin;

namespace NiSis
{
    /// <summary>
    /// Gestisce l'operazione di consolidamento
    /// </summary>
    public partial class Consolidamento : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MeseAnnoPicker1.SelectCommandVisible = false;
                MeseAnnoPicker1.SelectAll = true;
                //PopolateGrid();
            }
        }
        protected void PopolateGrid()
        {
            if (!MeseAnnoPicker1.HasSelectedItem())
                return;

            grdConsolida.Visible = true;
            VList<VConsolidaUtenze> ut = DataRepository.VConsolidaUtenzeProvider.Get(
                string.Format(
                "id_stato_lavorazione_utenza=2 and cd_storico_mensile_rete='{0}'", MeseAnnoPicker1.DateValue),
                "cd_storico_mensile_rete,cd_pool");
            grdConsolida.DataSource = ut;
            
            grdConsolida.DataBind();
            lblSTitle.Visible = grdConsolida.Rows.Count > 0;
            cmdConsolidate.Visible = grdConsolida.Rows.Count > 0;
            lblSTitle.Visible = grdConsolida.Rows.Count > 0;
            //cmdConsolidate.Visible = grdConsolida.Rows.Count > 0;
        }
        protected void cmdConsolidate_Click(object sender, EventArgs e)
        {
            try
            {
                DataRepository.VConsolidaUtenzeProvider.ConsolidateUsr("200801");
                CustomUtil.UpdatePoolStateAllLavorati(MeseAnnoPicker1.DateValue);
                PopolateGrid();
                Logger.Conferma("Operazione: Consolidamento utenze, esito:successo");
            }
            catch (Exception ex)
            {
                Logger.ErroreGrave("Operazione: Consolidamento utenze, esito:Errore nel Consolidamento Utenze", ex);
                throw;
            }
        }

        protected void cmdOk_Click(object sender, EventArgs e)
        {
            Response.Redirect("guess.aspx");
        }

        protected void cmdSelectShipper_Click(object sender, ImageClickEventArgs e)
        {
            PopolateGrid();
        }
    }
}
